package njust.struts2.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import njust.dao.MessageDao;
import njust.dao.RespondDao;
import njust.domain.Goods;
import njust.domain.Message;
import njust.domain.Respond;
import njust.domain.User;

import com.opensymphony.xwork2.ActionSupport;

public class RespondAction extends ActionSupport {

	RespondDao respondDao;
	String message;
	MessageDao messageDao;
	Respond respond;
	int messageId;

	public String addRespond() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");

		Message msg = messageDao.getMessageById(messageId);
		if (user == null || msg == null || respond .getContent().equals("")) {
			message = "不能为空";
			return "addRespondError";
		}

		respond.setUserid(user.getId());
		respond.setMessage(msg);

		if (respondDao.addRespond(respond)) {
			message = "添加成功";
			return "addRespondSuccess";
		} else {
			message = "添加失败";
			return "addRespondError";
		}
	}

	public String deleteRespond(int id) {
		if (respondDao.deleteRespond(id)) {
			message = "删除成功";
			return "deleteRespondok";
		} else {
			message = "删除失败";
			return "deleteResponderror";
		}
	}

	public String getAllRespond() {
		List responds = new ArrayList();
		responds = respondDao.getAllRespond();

		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("respondList", responds);
		return "getallRespondOK";
	}

	public String getRespondById(int id) {
		respond = respondDao.getRespondById(id);
		if (respond != null)
			return "getRespondOK";
		else
			return "getRespondError";
	}

	public RespondDao getRespondDao() {
		return respondDao;
	}

	public void setRespondDao(RespondDao respondDao) {
		this.respondDao = respondDao;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public Respond getRespond() {
		return respond;
	}

	public void setRespond(Respond respond) {
		this.respond = respond;
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

}
